package ca.scotthyndman.as2haxe.dom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.asdt.core.internal.antlr.AS2TokenTypes;

import ca.scotthyndman.as2haxe.dom.visitor.IVisitor;

/**
 * Represents an infix expression, which is two operands and an operator.
 */
public class ASInfixExpression extends ASExpression {

	/**
	 * The "leftOperand" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASChildPropertyDescriptor LEFT_OPERAND_PROPERTY = new ASChildPropertyDescriptor(
			ASInfixExpression.class, "leftOperand", ASExpression.class, true); //$NON-NLS-1$

	/**
	 * The "operator" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASSimplePropertyDescriptor OPERATOR_PROPERTY = new ASSimplePropertyDescriptor(
			ASInfixExpression.class,
			"operator", ASInfixExpression.Operator.class, true); //$NON-NLS-1$

	/**
	 * The "rightOperand" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASChildPropertyDescriptor RIGHT_OPERAND_PROPERTY = new ASChildPropertyDescriptor(
			ASInfixExpression.class, "rightOperand", ASExpression.class, true); //$NON-NLS-1$

	/**
	 * A list of property descriptors (element type:
	 * {@link ASPropertyDescriptor}), or null if uninitialized.
	 */
	private static final List<ASPropertyDescriptor> PROPERTY_DESCRIPTORS;

	static {
		List<Object> properyList = new ArrayList<Object>(4);
		createPropertyList(ASInfixExpression.class, properyList);
		addProperty(LEFT_OPERAND_PROPERTY, properyList);
		addProperty(OPERATOR_PROPERTY, properyList);
		addProperty(RIGHT_OPERAND_PROPERTY, properyList);
		PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
	}

	/**
	 * Returns a list of structural property descriptors for this node type.
	 * Clients must not modify the result.
	 * 
	 * @return a list of property descriptors
	 */
	public static List<ASPropertyDescriptor> propertyDescriptors() {
		return PROPERTY_DESCRIPTORS;
	}

	/**
	 * An infix operator.
	 */
	public enum Operator {
		TIMES("*", AS2TokenTypes.STAR),
		DIVIDE("/", AS2TokenTypes.DIV),
		MOD("%", AS2TokenTypes.MOD),
		PLUS("+", AS2TokenTypes.PLUS),
		MINUS("-", AS2TokenTypes.MINUS),
		LEFT_SHIFT("<<", AS2TokenTypes.SL),
		RIGHT_SHIFT(">>", AS2TokenTypes.SR),
		RIGHT_SHIFT_UNSIGNED(">>>", AS2TokenTypes.BSR),
		LESS("<", AS2TokenTypes.LT),
		LESS_EQ("<=", AS2TokenTypes.LE),
		GREATER(">", AS2TokenTypes.GT),
		GREATER_EQ(">=", AS2TokenTypes.GE),
		EQUAL("==", AS2TokenTypes.EQUAL),
		STRICT_EQUAL("===", AS2TokenTypes.STRICT_EQUAL),
		NOT_EQUAL("!=", AS2TokenTypes.NOT_EQUAL),
		STRICT_NOT_EQUAL("!==", AS2TokenTypes.STRICT_NOT_EQUAL),
		XOR("^", AS2TokenTypes.BXOR),
		AND("&", AS2TokenTypes.BAND),
		OR("|", AS2TokenTypes.BOR),
		LOGICAL_AND("&&", AS2TokenTypes.LAND),
		LOGICAL_OR("||", AS2TokenTypes.LOR);

		public static Map<Integer, Operator> typeToOperator;

		public final String token;

		public final int tokenType;

		Operator(String token, int tokenType) {
			this.token = token;
			this.tokenType = tokenType;

			init();
		}

		/**
		 * Initializes the operator
		 * 
		 */
		private void init() {
			if (typeToOperator == null) {
				typeToOperator = new HashMap<Integer, Operator>();
			}
			typeToOperator.put(this.tokenType, this);
		}

		/**
		 * Gets the operator associated with <code>tokenType</code>.
		 * 
		 * @param tokenType
		 *            The token type
		 * @return The operator
		 */
		public static Operator getOperator(int tokenType) {
			return typeToOperator.get(tokenType);
		}
	}

	private ASExpression leftOperand;

	private Operator operator;

	private ASExpression rightOperand;

	/**
	 * Constructs a new instance of ASInfixExpression
	 * 
	 * @param ast
	 *            The abstract syntax tree that owns this element
	 */
	public ASInfixExpression(AST ast) {
		super(ast);
	}

	/**
	 * Sets the left operand.
	 * 
	 * @param leftOperand
	 *            the new left operand
	 */
	public void setLeftOperand(ASExpression leftOperand) {
		if (leftOperand == null) {
			throw new IllegalArgumentException();
		}
		// an Assignment may occur inside a Expression - must check cycles
		ASTNode oldChild = this.leftOperand;
		preReplaceChild(oldChild, leftOperand, LEFT_OPERAND_PROPERTY);
		this.leftOperand = leftOperand;
		postReplaceChild(oldChild, leftOperand, LEFT_OPERAND_PROPERTY);
	}

	/**
	 * Returns the left operand.
	 * 
	 * @return the left operand
	 */
	public ASExpression getLeftOperand() {
		return leftOperand;
	}

	/**
	 * Sets the operator.
	 * 
	 * @param operator
	 */
	public void setOperator(Operator operator) {
		if (operator == null) {
			throw new IllegalArgumentException();
		}
		preValueChange(OPERATOR_PROPERTY);
		this.operator = operator;
		postValueChange(OPERATOR_PROPERTY);
	}

	/**
	 * Returns the operator
	 * 
	 * @return The operator
	 */
	public Operator getOperator() {
		return operator;
	}

	/**
	 * Sets the right operand.
	 * 
	 * @param rightOperand
	 *            the new right operand
	 */
	public void setRightOperand(ASExpression rightOperand) {
		if (rightOperand == null) {
			throw new IllegalArgumentException();
		}
		// an Assignment may occur inside a Expression - must check cycles
		ASTNode oldChild = this.rightOperand;
		preReplaceChild(oldChild, rightOperand, RIGHT_OPERAND_PROPERTY);
		this.rightOperand = rightOperand;
		postReplaceChild(oldChild, rightOperand, RIGHT_OPERAND_PROPERTY);
	}

	/**
	 * Returns the right operand.
	 * 
	 * @return the right operand
	 */
	public ASExpression getRightOperand() {
		return rightOperand;
	}

	//
	// ======== INTERNAL
	//

	/*
	 * (omit javadoc for this method) Method declared on ASTNode.
	 */
	final Object internalGetSetObjectProperty(
			ASSimplePropertyDescriptor property, boolean get, Object value) {
		if (property == OPERATOR_PROPERTY) {
			if (get) {
				return getOperator();
			} else {
				setOperator((Operator) value);
				return null;
			}
		}
		// allow default implementation to flag the error
		return super.internalGetSetObjectProperty(property, get, value);
	}

	/*
	 * (omit javadoc for this method) Method declared on ASTNode.
	 */
	final ASTNode internalGetSetChildProperty(
			ASChildPropertyDescriptor property, boolean get, ASTNode child) {
		if (property == LEFT_OPERAND_PROPERTY) {
			if (get) {
				return getLeftOperand();
			} else {
				setLeftOperand((ASExpression) child);
				return null;
			}
		}
		if (property == RIGHT_OPERAND_PROPERTY) {
			if (get) {
				return getRightOperand();
			} else {
				setRightOperand((ASExpression) child);
				return null;
			}
		}
		// allow default implementation to flag the error
		return super.internalGetSetChildProperty(property, get, child);
	}

	// ===== TO STRING AND VISITOR

	@Override
	public String toString() {
		return getLeftOperand().toString() + " " + getOperator().token + " "
				+ getRightOperand().toString();
	}

	@Override
	public void accept(IVisitor as2Visitor, boolean recursive) {
		recursive = as2Visitor.visit(this);

		if (!recursive) {
			return;
		}

		getLeftOperand().accept(as2Visitor, recursive);
		getRightOperand().accept(as2Visitor, recursive);
	}
}
